const express = require('express');
const query = require('./routes/query')
const demo = require('./routes/demo')
const queryByTrainNo = require('./routes/queryByTrainNo')
const stations = require('./routes/stations')
const secret = require('./routes/secret')

const app = express();
// 解析post请求传递的参数
app.use(express.urlencoded())

app.all('*', function (req, res, next) {
    // 设置可跨域访问
    res.header('Access-Control-Allow-Origin', '*');
    res.header('Access-Control-Allow-Headers', 'Content-Type, Content-Length, Authorization, Accept, X-Requested-With , yourHeaderFeild');
    res.header('Access-Control-Allow-Methods', 'PUT, POST, GET, DELETE, OPTIONS');

    if (req.method == 'OPTIONS') {
        res.send(200); // 让options请求快速返回
    }
    else {
        next();
    }
});

// 设置路由(接口名)
app.use('/query', query);
app.use('/demo', demo)
app.use('/queryByTrainNo', queryByTrainNo)
app.use('/stations', stations)
app.use('/secret', secret)

// 可以设置成动态导入并且根据文件目录自动设置接口名 TODO
const bookingOrders = require('./routes/bookingOrders/bookingOrders')
app.use('/bookingOrders', bookingOrders)

const getLoginBanner = require('./routes/initJSESSIONID/getLoginBanner')
app.use('/initJSESSIONID/getLoginBanner', getLoginBanner)
const passport = require('./routes/scanCodeLogin/passport')
app.use('/scanCodeLogin/passport', passport)
const uamauthclient = require('./routes/scanCodeLogin/uamauthclient')
app.use('/uamauthclient', uamauthclient)

const gettoken = require('./routes/qywx/gettoken')
app.use('/qywx/gettoken', gettoken)

const send = require('./routes/qywx/send')
app.use('/qywx/send', send)

const getClientIp = require('./routes/getClientIp')
app.use('/getClientIp', getClientIp)

module.exports = app;
